package com.peng.mvvm_wan.fragments

import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.ArrayAdapter
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.observe
import com.peng.mvvm_wan.R
import com.peng.mvvm_wan.adapters.SearchAdapter
import com.peng.mvvm_wan.base.BaseFragment
import com.peng.mvvm_wan.databinding.FragmentSearchBinding
import com.peng.mvvm_wan.viewmodels.SearchModel
import com.peng.safety.utils.InputManger
import kotlinx.android.synthetic.main.fragment_search.*

class SearchFragment : BaseFragment<SearchModel>() {
    private lateinit var mAdapter: SearchAdapter

    override fun getBindingView(): View? {
        val binding = FragmentSearchBinding.inflate(layoutInflater)
        binding.lifecycleOwner = this
        binding.searchModel = viewModel
        return binding.root
    }

    override fun getResourceId(): Int = R.layout.fragment_search

    override fun initViewModel(): SearchModel {
        return ViewModelProvider(requireActivity()).get(SearchModel::class.java)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel.selections.observeForever {
            search_text?.setAdapter(ArrayAdapter<String>(requireContext(),android.R.layout.simple_list_item_1,it.toList()))
        }
        btn_start_search?.setOnClickListener {
            val inputKey = search_text.text.toString().trim()
            if (!inputKey.isNullOrEmpty()) {
                viewModel.selections.value?.add(inputKey)
                viewModel.selections.value = viewModel.selections.value
            }
            viewModel.doSearch()
            InputManger.hideKeyBoard(requireContext(), search_text)
        }

        search_text.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                viewModel.doSearch()
                InputManger.hideKeyBoard(requireContext(), search_text)
            }
            false
        }

        search_text.addTextChangedListener {
            viewModel.inputKey.value = it?.toString()
        }

        mAdapter = SearchAdapter()
        mAdapter.loadMoreModule.setOnLoadMoreListener {
            viewModel.loadMore()
        }
        mAdapter.loadMoreModule.preLoadNumber = 5
        rv_search_result.adapter = mAdapter


        viewModel.searchResultList.observe(requireActivity()) {
            if (!mAdapter.hasEmptyView()) {
                mAdapter.setEmptyView(R.layout.layout_empty)
            }
            mAdapter.setList(it)
            if (srl_refresh.isShown) {
                srl_refresh.finishRefresh()
            }
        }

        viewModel.loadMoreStatus.observeForever { it ->
            when (it!!) {
                SearchModel.LoadStatus.COMPLETE -> mAdapter.loadMoreModule.loadMoreComplete()
                SearchModel.LoadStatus.END -> mAdapter.loadMoreModule.loadMoreEnd()
                SearchModel.LoadStatus.FAIL -> mAdapter.loadMoreModule.loadMoreFail()
            }
        }

        srl_refresh.setOnRefreshListener {
            viewModel.doSearch()
        }
        srl_refresh.setPrimaryColorsId(R.color.bg_light_color)
    }
}